package com.wzl.customviewproject.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 作者：longxiansen
 * 功能：
 */
public class MyLinearLayout extends LinearLayout {
    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    float flagY = 0f;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        RecyclerView childView = (RecyclerView) getChildAt(0);
        Log.d("MOVVE", "onInterceptTouchEvent: "+childView.hashCode()+"\n个数:"+childView.getAdapter().getItemCount());
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("ACTIONLOG","down");
                getParent().requestDisallowInterceptTouchEvent(true);
                flagY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d("ACTIONLOG","move");
                float moveY = ev.getY();
                if ((moveY - flagY)<0) {
                    //向上滑动
                    if (!childView.canScrollVertically(1)) {
                        Log.d("MOVVE", "上滑，到底部了");
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }else {
                        Log.d("MOVVE", "上滑，没到底部了");
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                } else if ((moveY - flagY) > 0) {
                    if (!childView.canScrollVertically(-1)) {
                        Log.d("MOVVE", "下滑，到顶部了");
                        getParent().requestDisallowInterceptTouchEvent(false);
//                        childView.scrollToPosition(0);//列表控件回到顶部
                    }else {
                        Log.d("MOVVE", "下滑，没到顶部了");
                        getParent().requestDisallowInterceptTouchEvent(true);
                    }
                }else {
                    Log.d("MOVVE","相等");
                    getParent().requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                Log.d("ACTIONLOG","up");
                getParent().requestDisallowInterceptTouchEvent(true);
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
